<?
//<meta http-equiv="content-type" content="text/html; charset=utf-8">
include "head.php";
include_once "umls_toolbox.php";

$tval = 0;
timestamp_init($tval);
$lap_array = array();

if ( $_REQUEST[cmd] == "init" ) {
	$query = "truncate {$table_prefix}temporary_concept";
	$result = mysql_query($query);

} else if ( $_REQUEST[cmd] == "make_subset" ) {
	
	$option = array();
	if ( $_REQUEST[adding_connective_concept] ) $option['adding_connective_concept'] = true;
	if ( $_REQUEST[adding_rel_rn] ) $option['adding_rel_rn'] = true;
	
	$lap_array[] = "option: ".print_r($option, true);

	$cui_str_list = array();
	$cui_str_list["S0000001"] = "Single Concept";
	
	$tree = array();			
	$fcui_val_list = array();	
	$rcui_list = array(); 	
	$single_cui_list = array();

	$bcui_list = array();
	$query = "select * from {$table_prefix}temporary_concept"; 
	$result = mysql_query($query) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	while( $row = mysql_fetch_array($result) ) {
		$cui = $row['cui'];
		$bcui_list[$cui] = true;
	}
	mysql_free_result($result);	
	$single_cui_list = $bcui_list;
	debug_r("bcui_list", $bcui_list);
	$lap_array[] = timestamp_lap($tval, "load temporary_concept");

	$query = "select a.* from {$table_prefix}concept_tree a join {$table_prefix}temporary_concept b on ( a.cui = b.cui )"; 
	$result = mysql_query($query) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	while( $row = mysql_fetch_array($result) ) {
		$cui_array = array();
		foreach ( explode("_", $row['fcui']) as $cui ) {
			if ( !$bcui_list[$cui] ) continue;
			$cui_array[] = $cui;
			$cui_str_list[$cui] = $row['str'];
		}
		$rcui = implode("_", $cui_array);
		$rcui_list[$rcui] = true;
	}
	mysql_free_result($result);	
	debug_r("rcui_list", $rcui_list);
	$lap_array[] = timestamp_lap($tval, "load concept_tree");

	if ( $option['adding_rel_rn'] ) {
		$query = "select cui1, cui2 from MRREL a join {$table_prefix}temporary_concept b on ( a.cui1 = b.cui ) and a.rel = 'RN'"; 
		$result = mysql_query($query) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
		while( $row = mysql_fetch_array($result) ) {
			$cui1 = $row['cui1'];
			$cui2 = $row['cui2'];
			if ( !$bcui_list[$cui2] ) continue;
			$rcui = $cui1."_".$cui2;
			$rcui_list[$rcui] = true;
		}
		debug_r("rcui_list", $rcui_list);
		$lap_array[] = timestamp_lap($tval, "load MRREL");
	}

	$child_rel_list = array();
	foreach ( $rcui_list as $rcui => $val ) {
		$cui_array = explode("_", $rcui);
		if ( count($cui_array) == 1 ) {
			$cui1 = $cui_array[0];
			$child_rel_list[$cui1] = array();
		} else {
			for ( $i = 0 ; $i < count($cui_array)-1 ; $i++ ) {
				for ( $j = $i+1 ; $j < count($cui_array) ; $j++ ) {
					$cui1 = $cui_array[$i];
					$cui2 = $cui_array[$j];
					if ( $cui1 == $cui2) continue;
					$child_rel_list[$cui1][$cui2] = true;
				}
			}
		}
	}
	$lap_array[] = timestamp_lap($tval, "make child_rel_list");

	$prev_list = array();
	$cycle_list = array();
	$done_list = array();
	foreach ( $child_rel_list as $cui => $val ) {
		if ( $done_list[$cui] ) continue;
		find_cycle_list($child_rel_list, $prev_list, $cui, $cycle_list, $done_list);
	}
	foreach ( $cycle_list as $cycle_node_str => $cui1 ) {
		$cycle_node_array = explode("_", $cycle_node_str);
		$cycle_node_cnt = count($cycle_node_array);
		for ( $i = 1 ; $i < $cycle_node_cnt ; $i++ ) {
			$cui1 = $cycle_node_array[$i];
			for ( $j = $i - 1 ; $j >= 0 ; $j-- ) {
				$cui2 = $cycle_node_array[$j];
				if ( $child_rel_list[$cui1][$cui2] ) $child_rel_list[$cui1][$cui2] = false;
			}
		}
	}
	debug_r("child_rel_list", $child_rel_list);
	$lap_array[] = timestamp_lap($tval, "remove cycle");
	
	foreach ( $child_rel_list as $cui1 => $cui1_val ) {
		foreach ( $cui1_val as $cui2 => $val ) {
			if ( $val ) {
				$single_cui_list[$cui1] = false;				
				$single_cui_list[$cui2] = false;
			}
		}
	}
	debug_r("single_cui_list", $single_cui_list);
	$lap_array[] = timestamp_lap($tval, "check single_cui_list");
	
	$tree = make_optimized_hierarchy($child_rel_list);	
	debug_r("tree", $tree);
	$lap_array[] = timestamp_lap($tval, "run longest path selection");

	if ( $option['adding_connective_concept'] ) {

		// 최상위 애들만 연결함
		$tcui_array = array();
		foreach ( $tree as $cui => $val ) {
			if ( $cui != "_value" ) $tcui_array[] = $cui;
		}
		foreach ( $single_cui_list as $cui => $val ) {
			if ( $val ) $tcui_array[] = $cui;
		}
		$where_in_cui = make_in_query($tcui_array);

		$prcui_val_list = array();
		$query = "select * from {$table_prefix}concept_tree where cui in ($where_in_cui)";
		$result = mysql_query($query) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
		while( $row = mysql_fetch_array($result) ) {
		
			$cui_array = explode ("_", $row['fcui']);
			$str_array = explode ("_", $row['fstr']);
			
			$rcui = "";
			for ( $i = 0 ; $i < count($cui_array) ; $i++ ) {
				$cui = $cui_array[$i];
				$str = $str_array[$i];
				$cui_str_list[$cui] = $str;
				
				if ( $rcui ) $rcui .= "_";
				$rcui .= $cui;
			}
			if ( $rcui ) $prcui_val_list[$rcui] = true;
		}
		mysql_free_result($result);
		$lap_array[] = timestamp_lap($tval, "make prcui_val_list from concept_tree");

		$ptree = make_tree_parse_list($prcui_val_list, "_");
		$prcui_val_list = find_connective_concept("", $ptree, 0);
		debug_r("prcui_val_list", $prcui_val_list);
		$lap_array[] = timestamp_lap($tval, "find connective concept");

		if ( $prcui_val_list ) {

			$prev_child_rel_list = $child_rel_list;
			foreach ( $prcui_val_list as $prcui => $val ) {
				$pcui_array = explode("|", $prcui);
				$last_cui = array_pop($pcui_array);
				if ( $prev_child_rel_list[$last_cui] ) {
					foreach ( $pcui_array as $cui1 ) {
						foreach ( $prev_child_rel_list[$last_cui] as $cui2 => $val ) {
							if ( !$val ) continue;
							if ( $cui1 == $cui2 ) continue;
							$child_rel_list[$cui1][$cui2] = true;
							$single_cui_list[$cui1] = false;				
							$single_cui_list[$cui2] = false;				
						}
					}
				} 
				foreach ( $pcui_array as $cui1 ) {
					if ( $cui1 == $last_cui ) continue;
					$child_rel_list[$cui1][$last_cui] = true;
					$single_cui_list[$cui1] = false;				
					$single_cui_list[$last_cui] = false;				
				}
			}
			debug_r("child_rel_list", $child_rel_list);
		
			$tree = make_optimized_hierarchy($child_rel_list);	
			debug_r("tree", $tree);
			$lap_array[] = timestamp_lap($tval, "run longest path selection");
		} 

	}
	foreach ( $single_cui_list as $cui => $val ) {
		if ( $val ) $tree["S0000001"][$cui] = array();
	}
	debug_r("single_cui_list", $single_cui_list);

	$child_cnt_list = array();
	update_child_cnt("", "", $tree, $child_cnt_list);
	debug_r("child_cnt_list", $child_cnt_list);
	$lap_array[] = timestamp_lap($tval, "update child cnt");

	$query = "alter table {$table_prefix}subset_concept_tree disable keys";
	mysql_query($query) or die("\n<br>$query\n<br>".mysql_error());
	$lap_array[] = timestamp_lap($tval, "disable subset_concept_tree keys");
	
	$query = sprintf("delete from {$table_prefix}subset_concept_tree where subset = '%s'", add_to_query($_REQUEST[query_subset]));
	mysql_query($query) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	$lap_array[] = timestamp_lap($tval, "delete subset_concept_tree");
		
	$ufstr_val_list = array(); 
	make_list_parse_tree($tree, $fcui_val_list, "_");	
	debug_r("fcui_val_list", $fcui_val_list);

	foreach ( $fcui_val_list as $idx => $idx_val ) {
		
		$cui = "";
		$str = "";
		$fcui = "";
		$fstr = "";
		$cui_array = explode("_", $idx);
		$depth = 0;
		foreach ( $cui_array as $cui ) {
			$str = $cui_str_list[$cui];
			
			if ( $fcui ) $fcui .= "_";
			$fcui .= $cui;

			if ( $fstr ) $fstr .= "_";
			$fstr .= $str;
			
			$depth++;
	
			$val = array();
			$val['cui'] = $cui;
			$val['str'] = $str;
			$val['fcui'] = $fcui;
			$val['fstr'] = $fstr;
			$val['depth'] = $depth;
			$ufstr_val_list[$fcui] = $val;
		}
	}
	//unset($fcui_val_list);
	$lap_array[] = timestamp_lap($tval, "make ufstr_val_list");

	foreach ( $ufstr_val_list as $fcui => $val ) {
		$cui = $val['cui'];
		if ( $cui == "TOP" ) continue;

		$tag = "";
		if ( $bcui_list[$cui] ) $tag = "";
		else $tag = "adding";

		$query = sprintf("insert into {$table_prefix}subset_concept_tree(subset,cui,str,fcui,fstr,depth,child_cnt,tag) values ('%s','%s','%s','%s','%s','%s','%s','%s')", 
			add_to_query($_REQUEST[query_subset]), $val['cui'], add_to_query($val['str']), $fcui, add_to_query($val['fstr']), $val['depth'], $child_cnt_list[$fcui], $tag);
		mysql_query($query) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	}
	//unset($cui_str_list);
	//unset($ufstr_val_list);
	$lap_array[] = timestamp_lap($tval, "insert subset_concept_tree");
	
	$query = "alter table {$table_prefix}subset_concept_tree enable keys";
	mysql_query($query) or die("\n<br>$query\n<br>".mysql_error());
	$lap_array[] = timestamp_lap($tval, "enable subset_concept_tree keys");


	$query = sprintf("delete from {$table_prefix}subset where subset = '%s'", add_to_query($_REQUEST[query_subset]));
	mysql_query($query) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	
	$query = sprintf("insert into {$table_prefix}subset(subset,note,update_time) values ('%s','%s','%s')", 
		add_to_query($_REQUEST[query_subset]), add_to_query(implode("\n", $lap_array)), date("Y-m-d H:i:s") );
	mysql_query($query) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
}

reload_opener();
close_window_msg("process complete");

include "tail.php";
